package com.v0710.tempro;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * @Author YIFan
 * @Date 2021-7-29 14:25
 * @Version 1.0
 */
public class GeneratebyMybatisplus {
    public static String auto(AppInfo appinfo){
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        //  1  * 全局配置
        GlobalConfig gc = new GlobalConfig();
        String outputDir = appinfo.getPath() + "/src/main/java/";
        gc.setOutputDir(outputDir);
        gc.setAuthor(appinfo.getAuthor());
        gc.setOpen(false);
        gc.setFileOverride(false);// 是否覆盖原来生成的
        gc.setServiceName("%sService");   // 去service的前缀
        gc.setIdType(IdType.ASSIGN_ID);
        gc.setDateType(DateType.ONLY_DATE);
        // gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);
        //  2 * 设置数据源
        DataSourceConfig config = new DataSourceConfig();
        config.setUrl("jdbc:mysql://"+appinfo.getDbPort()+"/"+appinfo.getDbName()+"?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8");
        config.setUsername(appinfo.getDbUsername());
        config.setPassword(appinfo.getDbPassword());
        config.setDriverName("com.mysql.cj.jdbc.Driver");
        config.setDbType(DbType.MYSQL);
        mpg.setDataSource(config);
        // 3 * 包的配置
        PackageConfig packageConfig = new PackageConfig();
        // 设置模块名
        packageConfig.setModuleName(appinfo.getModuleName());
        packageConfig.setParent(appinfo.getPackageName());
        outputDir = outputDir+appinfo.getPackageName();
        packageConfig.setEntity("entity");
        packageConfig.setMapper("mapper");
        packageConfig.setService("service");
        mpg.setPackageInfo(packageConfig);

        //  4 * 配置策略
        StrategyConfig strategy = new StrategyConfig();
        if (appinfo.getTableName().isEmpty()) {
            strategy.setInclude();// 设置要映射的表 &&&&&&&&&&&  不传参数是映射所有的表~
        }else {
            strategy.setInclude(appinfo.getTableName());// 设置要映射的表 &&&&&&&&&&&  不传参数是映射所有的表~
        }
        strategy.setNaming(NamingStrategy.underline_to_camel);  // 设置下划线 转 驼峰
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setLogicDeleteFieldName(appinfo.getDeletedField()); // 逻辑删除字段

        //  自动填充配置
        TableFill gmtCreate = new TableFill("create_time", FieldFill.INSERT);
        TableFill gmtModified = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(gmtCreate);
        tableFills.add(gmtModified);
        strategy.setTableFillList(tableFills);
        strategy.setVersionFieldName("version");// 乐观锁
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true); // localhost:8080/hello_id__2
        mpg.setStrategy(strategy);
        // 执行

        final ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(GeneratebyMybatisplus.class.getClassLoader());

        mpg.execute();
        Thread.currentThread().setContextClassLoader(oldContextClassLoader);
        System.err.println(("AutoGeneratorFun.auto===>[结束]"));

        return "成功，路径："+outputDir;
    }
}
